<?php

namespace App\Dao;

use App\Exceptions\SysExceptions;
use App\Model\UsersModel;
use Illuminate\Support\Facades\DB;

class UsersDao extends BaseDao
{
    public static function builder_query($params = []): \Illuminate\Database\Eloquent\Builder
    {
        $query = UsersModel::query();

        $query->when(checkIsHasField($params,'phone'),function ($query)use ($params){
            $query->where('phone',$params['phone']);
        });

        $query->when(checkIsHasField($params,'user_id'),function ($query)use ($params){
            $query->where('user_id',$params['user_id']);
        });

        $query->when(checkIsHasField($params,'spread_id'),function ($query)use ($params){
            $query->where('spread_id',$params['spread_id']);
        });

        $query->when(checkIsHasField($params,'vip_type'),function ($query)use ($params){
            $query->where('vip_type',$params['vip_type']);
        });

        return $query->orderByDesc('created_at');
    }


    /**
     * @throws SysExceptions
     */
    public static function login($params = []): array
    {
        checkForData(['phone','ver_code'],['请输入手机号','请输入验证码'],$params);
        $users = self::builder_query(['phone'=>$params['phone']]);
        if(!$users->exists()){
            $inserData = [
                'phone'=>$params['phone'],
                'nickname'=>'新视想用户',
                'head_img'=>'https://e-happy-tour.oss-cn-shenzhen.aliyuncs.com/20240424020101-1713924061.png',
                'srpead_id'=>0
            ];
            if(checkIsHasField($params,'spread_id')){
                $inserData['spread_id'] = $params['spread_id'];
            }

            if(checkIsHasField($params,'vip_type')){
                $inserData['vip_type'] = $params['vip_type'];
            }

            if(self::builder_query()->create($inserData) === false){
                self::error('登录失败');
            }
        }

        $info =  $users->first()->toArray();
        if($info['vip_type'] != $params['vip_type']){
            return  self::error('账号类型不正确');
        }
        return $info;
    }

    public static function getUserInfo(array $array): array
    {
        $info =  self::builder_query($array)->first()->toArray();
        $info['address'] = AddressDao::address_info(['user_id'=>$info['user_id']]);
        return $info;
    }

    public static function users(array $all): \Illuminate\Contracts\Pagination\LengthAwarePaginator
    {
        return self::builder_query($all)->paginate();
    }

    //用户充值
    public static function recharge(array $params)
    {
        try {
            BillsDao::moneysUpdate($params['money'],$params['user_id'],'系统充值');
            DB::commit();
        }catch (\Exception $exception){
            DB::rollBack();
            self::error($exception->getMessage());
        }
    }

    public static function updateHeadNIckname($wechatInfo,$user_id)
    {
        self::builder_query(['user_id'=>$user_id])->update([
            'nickname'=>$wechatInfo['nickname'],
            'head_img'=>$wechatInfo['headimgurl'],
        ]);
    }

    public static function spreads(array $params): array
    {
        $lists = self::builder_query($params)->get()->toArray();
        foreach ($lists as &$value){
            $value['xiaofeis'] = OrdersDao::builder_query([
                'user_id'=>$value['user_id']
            ])->where('status','>',0)->sum('price');
        }
        return  $lists;
    }
}
